/*
 * StartServer.java
 *
 * Created on February 20, 2007, 7:32 PM
 */
package d20chat;

import java.io.IOException;
import java.net.InetAddress;


/**
 * Displays a window to enter info to start a server instance.
 * Also displays the current internal IP address of the computer, this however
 * may not be the external IP address (this is provided to let users tell others
 * what address to connect to more easily).
 */
public class StartServer extends javax.swing.JFrame {
    
    private D20Chat tempMain;
    //private d20chatui tempGui;
    
    
    /** Creates new form StartServer */
/**
     * Class constructor for StartServer, displays window for user input.
     */
    /*public StartServer() {
        initComponents();
        
        // Center this jFrame window
        Dimension scrn = getToolkit().getScreenSize();
        this.setBounds( (scrn.width-getWidth())/2, (scrn.height-getHeight())/2, getWidth(), getHeight() );
        
        // Get local machine ip and store in serverIP text field
        try {
            InetAddress addr = InetAddress.getLocalHost();

            // Get IP Address
            byte[] ipAddr = addr.getAddress();
    
            // Convert to dot representation
            String ipAddrStr = "";
            for (int i=0; i<ipAddr.length; i++) {
                if (i > 0) {
                    ipAddrStr += ".";
                }
                ipAddrStr += ipAddr[i]&0xFF;
            }
        
            // Save  local IP as serverIP
            serverIPAddress.setText(ipAddrStr);
            
            // Set port to default
            serverPort.setText("4445");

            this.setVisible(false);
        } catch (IOException e) {
            
        }
    }
    */
    
/**
     * Class constructor for StartServer, displays window for user input.
     * Once the user enters info and hits start, a server will begin running.
     * @param main current instance of D20Chat.
     */
    public StartServer(D20Chat main) 
    {
        tempMain = main;
        //tempGui = gui;
        initComponents();
        if( !tempMain.theUser.getUsername().equals("noname") )
        {
            username.setText(tempMain.theUser.getUsername());
        }
        
        // Get local machine ip and store in serverIP text field
        try {
            InetAddress addr = InetAddress.getLocalHost();

            // Get IP Address
            byte[] ipAddr = addr.getAddress();
    
            // Convert to dot representation
            String ipAddrStr = "";
            for (int i=0; i<ipAddr.length; i++) {
                if (i > 0) {
                    ipAddrStr += ".";
                }
                ipAddrStr += ipAddr[i]&0xFF;
            }
        
            // Save  local IP as serverIP
            serverIPAddress.setText(ipAddrStr);
            
            // Set port to default
            serverPort.setText("4445");

            this.setVisible(false);
        } catch (IOException e) {
            
        }
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
/**
     * 
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        lblServerIPAddress = new javax.swing.JLabel();
        serverIPAddress = new javax.swing.JTextField();
        lblServerPort = new javax.swing.JLabel();
        serverPort = new javax.swing.JTextField();
        lblServerPassword = new javax.swing.JLabel();
        serverPassword = new javax.swing.JTextField();
        startServer = new javax.swing.JButton();
        cancel = new javax.swing.JButton();
        lblUsername = new javax.swing.JLabel();
        username = new javax.swing.JTextField();

        setTitle("Start Server");
        setName("StartServer");
        setResizable(false);
        lblServerIPAddress.setForeground(java.awt.Color.darkGray);
        lblServerIPAddress.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblServerIPAddress.setText("Server IP Address:");

        serverIPAddress.setEditable(false);

        lblServerPort.setText("Port:");

        lblServerPassword.setForeground(java.awt.Color.darkGray);
        lblServerPassword.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblServerPassword.setText("Server Password:");

        startServer.setText("Start Server");
        startServer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startServerActionPerformed(evt);
            }
        });

        cancel.setText("Cancel");
        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        lblUsername.setForeground(java.awt.Color.darkGray);
        lblUsername.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblUsername.setText("Username:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(startServer)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 77, Short.MAX_VALUE)
                        .addComponent(cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(lblServerIPAddress)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(serverIPAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblServerPort)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(serverPort, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblUsername)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(username, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(lblServerPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(serverPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblUsername)
                    .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(serverIPAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblServerIPAddress))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblServerPort)
                        .addComponent(serverPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(serverPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblServerPassword))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(startServer)
                    .addComponent(cancel))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    
/********************************************************************************
  Method:
  Description:
  Parameters:
  Used by:
  Uses:
********************************************************************************/
    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        // Hide form
        this.setVisible(false);
    }//GEN-LAST:event_cancelActionPerformed

    
/********************************************************************************
  Method:
  Description:
  Parameters:
  Used by:
  Uses:
********************************************************************************/
    private void startServerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startServerActionPerformed
        // Save serverIP, serverPort and serverPassword
        // Save server connection info
        if( username.getText().isEmpty() )
        {
            
        }
        else
        {
            try
            {
                tempMain.theUser.setUsername( username.getText().toString() );
            }
            catch( IllegalArgumentException iae )
            {
                String info = "client chat systemMessage client " + iae.getMessage();
                tempMain.InParse.CheckType( info );
            }
        }
        tempMain.serverIP = serverIPAddress.getText().toString();
        tempMain.serverPassword = serverPassword.getText().toString();
        tempMain.serverPort = Integer.parseInt(serverPort.getText().toString());
        tempMain.BeginServer();

        //tempGui.showMenuStartServer(false);
        //tempGui.showMenuStopServer(true);
        
        this.setVisible(false);
    }//GEN-LAST:event_startServerActionPerformed

    
/**
     * 
     */    
    
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancel;
    private javax.swing.JLabel lblServerIPAddress;
    private javax.swing.JLabel lblServerPassword;
    private javax.swing.JLabel lblServerPort;
    private javax.swing.JLabel lblUsername;
    private javax.swing.JTextField serverIPAddress;
    private javax.swing.JTextField serverPassword;
    private javax.swing.JTextField serverPort;
    private javax.swing.JButton startServer;
    private javax.swing.JTextField username;
    // End of variables declaration//GEN-END:variables
    
}
